from flask import current_app, jsonify
from flask import g
from flask import session
import functools
from info.utitls.response_code import RET


def set_rank_class(index):
    """根据传入的不用下标 返回不同的样式字符串"""
    if index == 1:
        return "first"
    if index == 2:
        return 'second'
    if index == 3:
        return "third"
    else:
        return ""


def user_login_data(view_func):
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):

        # 1.查询用户信息进行展示

        # 获取当前用户的user_id
        user_id = session.get('user_id')
        # 根据user_id 查询用户对象
        user = None  # 定义在外边是为了在后续字典中能使用
        from info.models import User
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
                return jsonify(errno=RET.DBERR, errmsg='查询用户异常')
        g.user = user
        result = view_func(*args, **kwargs)
        return result
    return wrapper